package com.cskaoyan.service;



import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.cskaoyan.bean.SmsSendResult;
import com.cskaoyan.configuration.properties.WdAliyunProperties;

import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author stone
 * @date 2023/04/21 20:09
 */
@Service
public class SmsServiceImpl implements SmsService{
    @Autowired
    WdAliyunProperties aliyunProperties;
    @Override
    public String send(String mobile, String code) {
        String accessKeyId = aliyunProperties.getAccessKeyId();
        String accessKeySecret = aliyunProperties.getAccessKeySecret();
        String signName = aliyunProperties.getSms().getSignName();
        String templateCode = aliyunProperties.getSms().getTemplateCode();
        /*String accessKeyId = "LTAI5t8gpxPTCR6W58RnZq4u";
        String accessKeySecret = "mVPbpbxia0JQotb7HyJAREV8QUuq8h";
        String signName = "王道训练营";
        String templateCode = "SMS_173765187";*/
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-qingdao") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();

        String templateParam = "{\"code\":\"" + code + "\"}";
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers(mobile)
                .signName(signName)
                .templateCode(templateCode)
                .templateParam(templateParam)
                .build();

        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = null;
        try {
            resp = response.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        String jsonResult = new Gson().toJson(resp);
        System.out.println(jsonResult);
        client.close();
        return jsonResult;
    }

    @Override
    public SmsSendResult sendValidationCode(String mobile) {
        // 生成验证码
        SecureRandom random = new SecureRandom();
        String code = String.valueOf(random.nextInt(899999) + 100000);
        String jsonResult = this.send(mobile, code);
        return new SmsSendResult(jsonResult,code);
    }
}
